import { useState, useRef, useEffect } from 'react'
import {
  getGoodsList,
  getGoodFineness,
  getRentPlan,
  postGoodsUpDownShelf,
  deleteGood,
  getGoodStatusCount,
  getSkuStock,
  updateSkuStock,
  getGainSet,
  getAddressList,
  saveReturnAddress,
  getGoodsClass,
  saveGood,
  getPlatformGoodsList,
  goodsDetail,
  getShopSelections,
  updateGoodsDisplayStatus,
  getPlatformGoodsCount,
  getCategorySelections,
  setGoodsCategory,
  getGoodsForDisplay,
  getForceRefuseReasons,
  forceGoodsDown,
  updateGoodsSort,
  saveAndActiveGoods,
  getShowPlts,
  getTerms,
} from '@/services/Goods'

export default () => {
  const [Spu, SetSpu] = useState([])
  const [Rent, SetRent] = useState([])
  const [Sell, SetSell] = useState([])
  const [Package, SetPackage] = useState([])
  const [Disabled, SetDisable] = useState(false)
  const FormRef = useRef()

  const SetFormRef = (ref: any) => {
    FormRef.current = ref
  }

  useEffect(() => {
    SetPackage(Rent.concat(Sell).filter(i => i))
  }, [Sell, Rent])

  // 获取商品列表
  const GetGoodsList = (data: any) => {
    return getGoodsList(data)
  }

  // 获取商品成色列表
  const GetGoodFineness = () => {
    return getGoodFineness()
  }

  // 获取租赁套餐列表
  const GetRentPlan = () => {
    return getRentPlan()
  }

  // 获取增值服务列表
  const GetGainSet = () => {
    return getGainSet()
  }

  // 获取归还地址列表
  const GetAddressList = (params: { merchant_id: string }) => {
    return getAddressList(params)
  }

  // 获取商品分类
  const GetGoodsClass = () => {
    return getGoodsClass()
  }

  // 新增归还地址列表
  const SaveReturnAddress = (data: any) => {
    return saveReturnAddress(data)
  }

  // 商品上下架
  const PostGoodsUpDownShelf = (param: any) => {
    return postGoodsUpDownShelf(param)
  }

  // 删除
  const DeleteGoods = (id: number) => {
    return deleteGood({ id })
  }

  // 获取商品特定状态的数量
  const GetGoodStatusCount = () => {
    return getGoodStatusCount()
  }

  // 获取规格库存
  const GetSkuStock = (id: number) => {
    return getSkuStock({ id })
  }

  // 更新商品的指定规格库存
  const UpdateSkuStock = (param: any) => {
    return updateSkuStock(param)
  }

  // 保存商品
  const SaveGood = (data: any) => {
    return saveGood(data)
  }

  // 保存商品并上架
  const SaveAndActiveGoods = (data: any) => {
    return saveAndActiveGoods(data)
  }

  // 获取商品详情
  const GetGoodsDetail = (data: any) => {
    return goodsDetail(data)
  }

  // 获取平台端商品列表
  const GetPlatformGoodsList = (data: any) => {
    return getPlatformGoodsList(data)
  }

  // 获取店铺列表
  const GetShopSelections = (data: any) => {
    return getShopSelections(data)
  }

  // 商品展示/取消展示（支持批量）
  const UpdateGoodsDisplayStatus = (data: any) => {
    return updateGoodsDisplayStatus(data)
  }

  // 【平台端】各个商品列表记录总数
  const GetPlatformGoodsCount = () => {
    return getPlatformGoodsCount()
  }

  // 【平台端】获取商品业务分类列表
  const GetCategorySelections = (params: { cate_name: string; plt: string | number }) => {
    return getCategorySelections(params)
  }

  // 【平台】设置商品分类
  const SetGoodsCategory = (data: any) => {
    return setGoodsCategory(data)
  }

  // 【平台】设置商品分类
  const GetGoodsForDisplay = (params: any) => {
    return getGoodsForDisplay(params)
  }

  // 【平台】强制下架原因选项
  const GetForceRefuseReasons = () => {
    return getForceRefuseReasons()
  }

  // 【平台】强制下架
  const GorceGoodsDown = <D>(data: D) => {
    return forceGoodsDown(data)
  }

  // 【平台】强制下架
  const UpdateGoodsSort = <D>(data: D) => {
    return updateGoodsSort(data)
  }

  // 获取展示渠道列表
  const GetShowPlts = (params: any) => {
    return getShowPlts(params)
  }

  // 获取买卖套餐租期数列表
  const GetTerms = (params: any) => {
    return getTerms(params)
  }


  return {
    GetGoodsList,
    GetGoodFineness,
    GetRentPlan,
    GetGainSet,
    PostGoodsUpDownShelf,
    DeleteGoods,
    GetGoodStatusCount,
    GetSkuStock,
    UpdateSkuStock,
    GetAddressList,
    SaveReturnAddress,
    GetGoodsClass,
    SaveGood,
    GetGoodsDetail,
    GetPlatformGoodsList,
    GetShopSelections,
    UpdateGoodsDisplayStatus,
    GetPlatformGoodsCount,
    GetCategorySelections,
    SetGoodsCategory,
    GetGoodsForDisplay,
    GetForceRefuseReasons,
    GorceGoodsDown,
    UpdateGoodsSort,
    SaveAndActiveGoods,
    GetShowPlts,
    GetTerms,

    Spu,
    SetSpu,
    Rent,
    SetRent,
    Sell,
    SetSell,
    Package,
    FormRef,
    SetFormRef,
    Disabled,
    SetDisable
  }
}
